#include <iostream>
#include <string>
#include <vector>
#include <boost/filesystem.hpp>
#include "util.hpp"

const std::string src_path = "data/input";
const std::string output = "data/raw_html/raw.txt";

typedef struct DocInfo{
    std::string title;
    std::string content;
    std::string url;
}DocInfo_t;

bool EnumFile(const std::string& src_path, std::vector<std::string>* files_list);
bool ParseFile(const std::vector<std::string>& files_list, std::vector<DocInfo_t>* raw);
bool SaveHtml(const std::vector<DocInfo_t>& raw, const std::string& output);

int main()
{
    // 1. 递归式地将 html 文件带路径保存到 vector 中
    std::vector<std::string> files_list;
    if(!EnumFile(src_path, &files_list))
    {
        std::cerr << "enum file error!" << std::endl;
        return 1;
    }

    // 2.  清洗数据
    std::vector<DocInfo_t> raw;
    if(!ParseFile(files_list, &raw))
    {
        std::cerr << "parse file error!" << std::endl;
        return 2;
    }

    // 3. 写入到 output，按照 \3 分割
    if(!SaveHtml(raw, output))
    {
        std::cerr << "save html error!" << std::endl;
        return 3;
    }
    
    return 0;
}

// * -- 输出参数
// const & -- 输入输出参数
bool EnumFile(const std::string& src_path, std::vector<std::string>* files_list)
{
    namespace fs = boost::filesystem;
    fs::path root_path(src_path);
    // 检查目录是否存在
    if(!fs::exists(root_path))
    {
        std::cerr << src_path << "not exists" << std::endl;
        return false;
    }

    // 遍历data
    fs::recursive_directory_iterator end; // 空迭代器, 结束边界
    for(fs::recursive_directory_iterator it(root_path); it != end; it++)
    {
        // 不是普通文件跳过
        if(!fs::is_regular(*it)) continue;
        // 不是.html文件跳过
        if(it->path().extension() != ".html") continue;

        // std::cout << "log: " << it->path().string() << std::endl;
        files_list->push_back(it->path().string());
    }
    return true;
}

static bool ParseTitle(const std::string& file, std::string* title)
{
    size_t begin = file.find("<title>");
    if(begin == std::string::npos) return false;
    size_t end = file.find("</title>");
    if(end == std::string::npos) return false;
    begin += std::string("<title>").size();
    if(begin > end) return false;
    *title = file.substr(begin, end - begin);

    return true;
}
static bool ParseContent(const std::string& file, std::string* content)
{
    enum status{
        LABLE,
        CONTENT
    };
    enum status s = LABLE;
    for(char c : file)
    {
        switch (s)
        {
        case LABLE:
            if(c == '>') s = CONTENT;
            break;
        case CONTENT:
            if(c == '<') s = LABLE;
            else{
                if(c == '\n') c = ' ';
                content->push_back(c);
            }
            break;
        default:
            break;
        }
    }
    return true;
}
static bool ParseUrl(const std::string& file_path, std::string* url)
{
    std::string url_head = "https://www.boost.org/doc/libs/1_84_0/doc/html";
    std::string url_tail = file_path.substr(src_path.size());
    *url = url_head + url_tail;

    return true;
}

static void show(const DocInfo_t& doc)
{
    std::cout << "title: " << doc.title << std::endl;
    std::cout << "content: " << doc.content << std::endl;
    std::cout << "url: " << doc.url << std::endl;
}

bool ParseFile(const std::vector<std::string>& files_list, std::vector<DocInfo_t>* raw)
{
    for(const std::string& file : files_list)
    {
        // 读取文件
        std::string result;
        if(!ns_util::FileUtil::ReadFile(file, &result))
        {
            continue;
        }
        DocInfo doc;
        // 提取 title
        if(!ParseTitle(result, &doc.title))
        {
            continue;
        }

        // 提取 content (去标签)
        if(!ParseContent(result, &doc.content))
        {
            continue;
        }

        // 提取 URL
        if(!ParseUrl(file, &doc.url))
        {
            continue;
        }
        raw->push_back(std::move(doc)); // 拷贝, 效率低

        // debug
        // show(doc);
        // break;
    }
    return true;
}
bool SaveHtml(const std::vector<DocInfo_t>& raw, const std::string& output)
{
#define SEP '\3'
    // 按照二进制写入 
    std::ofstream out(output, std::ios::out | std::ios::binary);
    if(!out.is_open())
    {
        std::cerr << "open " << output << " failed!" << std::endl;
        return false;
    }
    for(auto& item : raw)
    {
        std::string out_string;
        out_string += item.title;
        out_string += SEP;
        out_string += item.content;
        out_string += SEP;
        out_string += item.url;
        out_string += '\n';

        out.write(out_string.c_str(), out_string.size());
    }
    out.close();

    return true;
}
